import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:rabbit_mobile/view/Category/models/category_model.dart';
import 'package:rabbit_mobile/view/Home/models/home_model.dart';
import 'package:rabbit_mobile/service/request.dart';
import 'package:rabbit_mobile/view/goods_details/models/goods_details_model.dart';
import 'package:rabbit_mobile/view/register/models/register_model.dart';

import '../view/Mine/models/user_profile_model.dart';
import '../view/Oder_payment/models/payments_model.dart';
import '../view/ShoppingCart/models/shopping_cart_model.dart';

class HomeAPI {
  /// 获取首页网络数据的接口方法
  static Future<HomeModel> homeFetch() async {
    Response response = await XTXRequestManager().handleRequest('home/index', 'GET');
    // 读取响应体数据中的result字段
    dynamic ret = response.data['result'];
    // 将result字段对应的首页网络数据传给首页总模型, 转模型数据
    HomeModel homeModel = HomeModel.fromJson(ret);
    return homeModel;
  }
}

/// 分類請求
class CategoryApi {
  /// 一級分類請求
  static Future categoryFetch() async {
    Response response = await XTXRequestManager().handleRequest('home/category/head/app', 'GET');
    dynamic ret = response.data;
    CategoryFirstLevel categoryFirstLevel = CategoryFirstLevel.fromJson(ret);
    return categoryFirstLevel;
  }

  /// 二級分類請求
  static Future categorySecondsLevelFetch(String id) async {
    String path = 'category?id=$id';
    Response response = await XTXRequestManager().handleRequest(path, 'GET');
    dynamic ret = response.data['result'];
    CategorySecondsLevel categorySecondsLevel = CategorySecondsLevel.fromJson(ret);
    return categorySecondsLevel;
  }
}

/// 登錄請求
class LoginApi<Map> {
  static Future loginFetch(dynamic data) async {
    Response response = await XTXRequestManager().handleRequest('login', 'POST', data: data);
    dynamic ret = response.data['result'];
    // LoginModel loginModel = LoginModel.fromJson(ret);
    return ret;
  }
}

/// 檢測手機號是否注冊
class RegisterPhoneNumberApi {
  static Future phoneNumberRegisterFetch(String number) async {
    String path = 'register/check/?account=$number';
    Response response = await XTXRequestManager().handleRequest(path, "GET");
    dynamic ret = response.data['result'];
    RegisterModel registerModel = RegisterModel.fromJson(ret);
    return registerModel;
  }
}

/// 獲取驗證碼
class GetSMSVerificationCode {
  static Future getSMSVerificationCodeFetch(String mobile, String type) async {
    String path = 'code/?mobile=$mobile&type=$type';
    Response response = await XTXRequestManager().handleRequest(path, "GET");
    dynamic ret = response.data['msg'];
    return ret;
  }
}

/// 驗證手機驗證碼
class CheckSMSVerificationCode {
  static Future checkSMSVerificationCodeFetch(String mobile, String code) async {
    String path = 'register/code/check/?mobile=$mobile&code=$code';
    Response response = await XTXRequestManager().handleRequest(path, "GET");
    dynamic ret = response.data['msg'];
    return ret;
  }
}

/// 注冊
class RegisterApi {
  static Future registerFetch(String mobile,
      String code,
      String account,
      String password,) async {
    Map data = {"account": account, "mobile": mobile, "code": code, "password": password, "type": "app"};
    Response response = await XTXRequestManager().handleRequest('register', "POST", data: data);
    dynamic ret = response.data['result'];
    return ret;
  }
}

/// 商品詳情
class GoodsDetailsApi {
  static Future goodsDetailsFetch(String id) async {
    String path = 'goods/app/?id=$id';
    Response response = await XTXRequestManager().handleRequest(path, "GET");
    dynamic ret = response.data['result'];
    GoodsDetailsModel goodsDetailsModel = GoodsDetailsModel.fromJson(ret);
    return goodsDetailsModel;
  }
}

/// ADDED SHOPPING CART API
class ShoppingCartApi {
  static Future shoppingCartFetch(dynamic data) async {
    String path = 'member/cart';
    Response response = await XTXRequestManager().handleRequest(path, "POST", data: data);
    dynamic ret = response.data['result'];
    return ret;
  }
}

/// get shopping cart data
class GetShoppingCartApi {
  static Future getShoppingCartFetch() async {
    String path = 'member/cart/mutli';
    Response response = await XTXRequestManager().handleRequest(path, "GET");
    dynamic ret = response.data['result'];
    ShoppingCartModel shoppingCartModel = ShoppingCartModel.fromJson(ret);
    return shoppingCartModel;
  }
}

/// change shoppingCart state
class ChangeShoppingCartApi {
  static Future changeShoppingCartFetch(dynamic data1) async {
    String path = 'member/cart/${data1["id"]}';
    Map data = {
      "selected": data1["selected"],
      "count": data1["count"],
    };
    Response response = await XTXRequestManager().handleRequest(path, "PUT", data: data);
    dynamic ret = response.data['result'];
    return ret;
  }
}

/// delete shoppingCart state
class DeleteShoppingCartApi {
  static Future deleteShoppingCartFetch(String ids) async {
    String path = 'member/cart';
    Map data = {
      "ids": [ids],
      "clearAll": false,
      "clearInvalid": false
    };
    Response response = await XTXRequestManager().handleRequest(path, "DELETE", data: data);
    dynamic ret = response.data['result'];
    return ret;
  }
}

/// GET SHOPPING CART COUNT
class GetShoppingCartCountApi {
  static Future getShoppingCartCountFetch() async {
    String path = 'member/cart/count';
    Response response = await XTXRequestManager().handleRequest(path, "GET");
    dynamic ret = response.data['result'];
    GetShoppingCartCountModel getShoppingCartCountModel = GetShoppingCartCountModel.fromJson(ret);
    return getShoppingCartCountModel;
  }
}

/// GUESS YOU LIKE
class GetGuessYouLike {
  static Future getGuessYouLikeFetch() async {
    String path = 'goods/relevant';
    Response response = await XTXRequestManager().handleRequest(path, "GET");
    dynamic ret = response.data;
    GetGuessYouLikeModel getGuessYouLikeModel = GetGuessYouLikeModel.fromJson(ret);
    return getGuessYouLikeModel;
  }
}

/// User Profile Information
class GetUserProfileInformation {
  static Future getUserProfileInformationFetch() async {
    String path = 'member/profile/check';
    Response response = await XTXRequestManager().handleRequest(path, "GET");
    dynamic ret = response.data['result'];
    // if (ret == null) {
    //   return null;
    // }
    // UserProfileInformationModel userProfileInformationModel = UserProfileInformationModel.fromJson(ret);
    return ret;
  }
}

/// payments
class PaymentsGoods {
  static Future paymentsGoodsFetch() async {
    String path = 'member/order/pre';
    Response response = await XTXRequestManager().handleRequest(path, "GET");
    dynamic ret = response.data['result'];
    PaymentsModel paymentsModel = PaymentsModel.fromJson(ret);
    return paymentsModel;
  }
}
